-- 怪物
--
local skynet     = require "skynet"
local datacenter = require "skynet.datacenter"
local cjson      = require "cjson"


local world
local id = ...

id = math.floor(tonumber(id))

local CMD = {}
local map_width, map_height

local x, y, sx, sy

local function move()
    x = x + sx
    y = y + sy
    if x < 0 then
        sx = -sx
        x = 0
    end
    if x > map_width then
        sx = -sx
        x = map_width
    end
    -- # 纵向出界
    if y <= 0 then --# 离开了地图下边
        sy = -sy
        y = 0
    end

    if y >= map_height then -- # 离开了地图上边
        sy = -sy
        y = map_height
    end
end

math.randomseed(tostring(os.time() + id):reverse():sub(1, 7))

local scene_ids = {1001, 1002, 1003}

function CMD.updateaoiobj(obj)
    -- skynet.error("monster updateaoiobj:",cjson.encode(obj))
    --skynet.error("monster agent function updateaoiobj=","my id=",id,",my post=",cjson.encode(POS),obj.tempid,",data=",cjson.encode(obj))
end

function CMD.delaoiobj(obj)
    -- skynet.error("monster delaoiobj:",cjson.encode(obj))
    --skynet.error("monster agent function delaoiobj=","my id=",id,",my post=",cjson.encode(POS),obj.tempid,",data=",cjson.encode(obj))
end

function CMD.addaoiobj(user_id, obj)
    -- skynet.error("monster addaoiobj user_id=",user_id,",obj=",cjson.encode(obj))
    --skynet.error("monster agent function addaoiobj=","my id=",id,",my post=",cjson.encode(POS),obj.tempid,",data=",cjson.encode(obj))
end

function CMD.updateaoilist(obj)
    -- skynet.error("monster agent function updateaoilist",obj.tempid,",data=",cjson.encode(obj))
end


function CMD.joingroup(data)
    skynet.error("group join id = ", id , " data=",cjson.encode(data))
end

function CMD.leavegroup(data)
    skynet.error("group leave  id = ", id , "  data=",cjson.encode(data))
end

function CMD.otherleavegroup(data)
    skynet.error("group otherleave id = ", id , "  data=",cjson.encode(data))
end


function CMD.start(conf)

    local total_count = math.random(100, 200)
    map_width = conf.width
    map_height = conf.height
    -- local ok = pcall(skynet.call,aoi,'lua','enter',conf)

    x = math.random(1, 10)
    y = math.random(1, 10)

    sx = math.random(10, 20)
    -- sy=math.random(10,20)
    sy = 0


    local r = math.random(1, 255)
    local g = math.random(1, 255)
    local b = math.random(1, 255)
    move()
    POS = {
        x = x,
        y = y,
        z = 0,
    }

    local scene_id = scene_ids[math.random(1,3)]
    skynet.error("scene_id = ", scene_id)
    -- local obj = {
    --     tempid = id,
    --     scene_id = scene_ids[math.random(1,3)],
    --     agent = skynet.self(),
    --     movement = {
    --         mode = "wm",
    --         pos = POS,
    --     },
    --     colors = { r, g, b },
    --     look_info = { r, g, b },
    --     type = 1,
    --     dir = sx,
    -- }
    local cmd = {
        data = {
            pos=POS,
            agent=skynet.self(),
            dir=sx,
            look_info = { id=id, name="name:"..id },
            colors = { r, g, b },
        }
    }
    local ok = pcall(skynet.call, world, 'lua', 'enter', id, cmd)

    skynet.fork(function()
        while true do
            local r1 = math.random(1, 2)
            local r2 = math.random(10, 100)
            local r3 = math.random(1, 2)


            move()
            POS = {
                x = x,
                y = y,
                z = 0,
            }
            -- local obj = {
            --     tempid = id,
            --     scene_id = scene_id,
            --     agent = skynet.self(),
            --     movement = {
            --         mode = "wm",
            --         pos = POS,
            --     },
            --     colors = { r, g, b },
            --     look_info = { r, g, b },
            --     type = 1,
            --     dir = sx,
            -- }
            local cmd = {
                data = {
                    agent=skynet.self(),
                    pos=POS,
                    dir=sx,
                    look_info = { id=id, name="name:"..id },
                    colors = { r, g, b },
                }
            }
            local ok = pcall(skynet.call, world, 'lua', 'move', id, cmd)
            skynet.sleep(5)
        end
    end)
    return "ok"
end

skynet.start(function()
    world = skynet.uniqueservice("world")
    skynet.dispatch("lua", function(_, _, command, ...)
        -- skynet.error("robot cmd = ", command)
        local f = assert(CMD[command])
        local ret = f(...)

        if ret then
            skynet.ret(skynet.pack(ret))
        end

    end)
    collectgarbage()
end)
